﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using EnvSafe.Expression;

namespace Heroius.DBTransitor
{
    public partial class FormMappingRule : Form
    {
        internal FormMappingRule(string[] FieldNames, List<MappingField> Fields)
        {
            InitializeComponent();
            Fn = FieldNames;
            CbFieldName.Items.AddRange((from f in Fields select f.Name).ToArray());
            Fs = Fields;
            CbFieldName.SelectedIndex = 0;
            CbFieldName.SelectedIndexChanged += CbFieldName_SelectedIndexChanged;
        }

        internal FormMappingRule(string[] FieldNames, MappingField Field)
        {
            InitializeComponent();
            Fn = FieldNames;
            Cf = Field;
            CbFieldName.Items.Add(Field.Name);
            CbFieldName.SelectedIndex = 0;
            CbFieldName.Enabled = false;
            //sure the field has rule, now that you can edit it.
            RbMapped.Checked = Cf.HasMapping;
            
            if (RbNoMap.Checked)
            {
                TbNos.Text = Cf.MappingRule.nos;
            }
            else
            {
                if (Cf.MappingRule.nu != null)
                {
                    ChbNu.Checked = true;
                    TbNus.Text = Cf.MappingRule.nus;
                }
                if (Cf.MappingRule.c != null)
                {
                    ChbCR.Checked = true;
                    TbCs.Text = Cf.MappingRule.cs;
                    TbRs.Text = Cf.MappingRule.rs;
                }
            }
        }

        string[] Fn;
        List<MappingField> Fs;
        MappingField Cf;

        //new only
        private void CbFieldName_SelectedIndexChanged(object sender, EventArgs e)
        {
            Cf = Fs.Find(f => f.Name == CbFieldName.SelectedItem.ToString());
            TbCs.Clear();
            RbMapped.Checked = Cf.HasMapping;
            RbNoMap.Checked = !RbMapped.Checked;
        }

        private void RbNoMap_CheckedChanged(object sender, EventArgs e)
        {
            BtnNo.Enabled = RbNoMap.Checked;
            panel1.Enabled = RbMapped.Checked;

            if (RbNoMap.Checked)
            {
                ChbNu.Checked = false;
                ChbCR.Checked = false;
            }
            else
            {
                TbNos.Clear();
            }
        }

        private void ChbNu_CheckedChanged(object sender, EventArgs e)
        {
            BtnNu.Enabled = ChbNu.Checked;
            if (!ChbNu.Checked)
            {
                TbNus.Clear();
            }
        }

        private void ChbCR_CheckedChanged(object sender, EventArgs e)
        {
            BtnC.Enabled = BtnR.Enabled = ChbCR.Checked;
            if (!ChbCR.Checked)
            {
                TbCs.Clear();
                TbRs.Clear();
            }
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            if (RbMapped.Checked && (!ChbCR.Checked) && (!ChbNu.Checked))
            {
                MessageBox.Show("应至少选择一个规则！");
                return;
            }


            Procedure no = null, nu = null, c = null, r = null;
            if (RbNoMap.Checked)
            {
                if (TbNos.Text == "")
                {
                    MessageBox.Show("已选中的规则不能为空！");
                    return;
                }
                no = Step3Helper.Maker.GenerateProcedure(TbNos.Text);
            }
            if (ChbNu.Checked)
            {
                if (TbNus.Text == "")
                {
                    MessageBox.Show("已选中的规则不能为空！");
                    return;
                }
                nu = Step3Helper.Maker.GenerateProcedure(TbNus.Text);
            }
            if (ChbCR.Checked)
            {
                if (TbCs.Text == "" || TbRs.Text == "")
                {
                    MessageBox.Show("已选中的规则不能为空！");
                    return;
                }
                c = Step3Helper.Maker.GenerateProcedure(TbCs.Text);
                r = Step3Helper.Maker.GenerateProcedure(TbRs.Text);
            }
            Cf.RemoveRule();
            Cf.DefineRule(TbNos.Text, TbNus.Text, TbCs.Text, TbRs.Text,
                no, nu, c, r);

            EventHandler<RuleUpdatedArg> a = RuleUpdated;
            if (a!=null)
            {
                a(this, new RuleUpdatedArg() { FieldName = Cf.Name });
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        internal event EventHandler<RuleUpdatedArg> RuleUpdated;

        internal class RuleUpdatedArg : EventArgs
        {
            internal string FieldName;
        }

        private void BtnNo_Click(object sender, EventArgs e)
        {
            FormExpressionBuilder b = new FormExpressionBuilder(Fn, TbNos.Text);
            b.OnProcedureGenerated += (s, ee) => {
                TbNos.Text = b.LatestExpression;
            };
            b.ShowDialog();
        }

        private void BtnNu_Click(object sender, EventArgs e)
        {
            FormExpressionBuilder b = new FormExpressionBuilder(Fn, TbNus.Text);
            b.OnProcedureGenerated += (s, ee) =>
            {
                TbNus.Text = b.LatestExpression;
            };
            b.ShowDialog();
        }

        private void BtnC_Click(object sender, EventArgs e)
        {
            FormExpressionBuilder b = new FormExpressionBuilder(new string[]{ Fn[Cf.MappingIndex]}, TbCs.Text);
            b.OnProcedureGenerated += (s, ee) =>
            {
                TbCs.Text = b.LatestExpression;
            };
            b.ShowDialog();
        }

        private void BtnR_Click(object sender, EventArgs e)
        {
            FormExpressionBuilder b = new FormExpressionBuilder(Fn, TbRs.Text);
            b.OnProcedureGenerated += (s, ee) =>
            {
                TbRs.Text = b.LatestExpression;
            };
            b.ShowDialog();
        }
    }
}
